package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.UserAccountService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("rbac/account")
@Menu("权限管理系统.用户账号管理")
public class UserAccountController {
	
	@Autowired
	UserAccountService userAccountService;
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@GetMapping("page")
	@Permission("分页查询")
	public Result page(Model params){
		Page page=userAccountService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	/**
	 * 新增账号
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增账号")
	public Result add(Model params){
		if(userAccountService.getAccountByLoginName(params.getString("loginAccount"))!=null){
			return Result.Error("RBAC_USER_ACCOUNT_CHECK");
		}
		if(userAccountService.add(params)){
			return Result.Success("RBAC_USER_ACCOUNT_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_ACCOUNT_ADD_ERROR");
		}
	}
	
	/**
	 * 启用禁用
	 * @param request
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用")
	public Result checkState(String id){
		if(userAccountService.checkState(id)){
			return Result.Success("RBAC_USER_ACCOUNT_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_ACCOUNT_CHECKSTATE_ERROR");
		}
	}
}
